﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task1_Sum
{
    class Task1_Sum
    {
        static void Main(string[] args)
        {
            int degree;
            GetNumberFromInput(out degree, "degree");
            int argument;
            GetNumberFromInput(out argument, "argument");

            double sum = CalculateSum(degree, argument);
            Console.WriteLine();
            Console.WriteLine("The calculated sum is {0}.", sum);
        }

        private static void GetNumberFromInput(out int value, string number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            Console.Write("Please, enter the "+ number +" of the polynom: ");
            string inputLine = Console.ReadLine();
            bool parseStringResult = int.TryParse(inputLine, out value);
            while (!parseStringResult || value < 0)
            {
                Console.Write("Incorrect input! Please, enter the " + number + " of the polynom: ");
                inputLine = Console.ReadLine();
                parseStringResult = int.TryParse(inputLine, out value);
            }
        }

        private static double CalculateSum(int degree, int argument)
        {
            if (degree == null)
            {
                throw new NullReferenceException();
            }
            if (argument == null)
            {
                throw new NullReferenceException();
            }
            if (degree < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            double sum = 1;
            double currentMonomial = 1;
            int currentDegree = 1;
            while (currentDegree <= degree)
            {
                currentMonomial = currentMonomial * currentDegree;
                currentMonomial = currentMonomial / argument;
                sum += currentMonomial;
                currentDegree++;
            }
            return sum;
        }
    }
}
